package com.zb.controller.settings;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zb.common.annotation.Permission;
import com.zb.common.po.enums.LogType;
import com.zb.common.po.enums.ResponseCode;
import com.zb.common.po.model.OperationLog;
import com.zb.common.po.query.OperationLogQuery;
import com.zb.common.po.vo.AjaxResponse;
import com.zb.common.utils.PaginationResult;
import com.zb.service.OperationLogService;

@Controller
@RequestMapping(value = "/settings")
public class OperationLogSettingsController {

    Logger logger = LoggerFactory
            .getLogger(OperationLogSettingsController.class);

    @Resource
    private OperationLogService operationLogService;

    @Permission(code = "oplog:list")
    @RequestMapping(value = "/opLog_list.do")
    public String opLog_list() {
        return "page/settings/log_list";
    }

    @Permission(code = "oplog:list")
    @ResponseBody
    @RequestMapping(value = "/loadOpLogList.do")
    public AjaxResponse<PaginationResult<OperationLog>> loadOpLogList(
            HttpSession session, OperationLogQuery query) {
        AjaxResponse<PaginationResult<OperationLog>> response = new AjaxResponse<PaginationResult<OperationLog>>();
        try {
            query.setLogType(LogType.OPERATION);
            PaginationResult<OperationLog> result = operationLogService
                    .findLogListByPage(query);
            response.setData(result);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("查询操作日志异常：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("查询日志异常");
        }
        return response;
    }

    @Permission(code = "loginLog:list")
    @RequestMapping(value = "/loginLog_list.do")
    public ModelAndView loginLog_list() {
        ModelAndView view = new ModelAndView("page/settings/log_list");
        view.addObject("type", LogType.LOGIN.getType());
        return view;
    }

    @Permission(code = "loginLog:list")
    @ResponseBody
    @RequestMapping(value = "/loadLoginLogList.do")
    public AjaxResponse<PaginationResult<OperationLog>> loadLoginLogList(
            HttpSession session, OperationLogQuery query) {
        AjaxResponse<PaginationResult<OperationLog>> response = new AjaxResponse<PaginationResult<OperationLog>>();
        try {
            query.setLogType(LogType.LOGIN);
            PaginationResult<OperationLog> result = operationLogService
                    .findLogListByPage(query);
            response.setData(result);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("查询操作日志异常：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("查询日志异常");
        }
        return response;
    }
}
